<style>
    .layui-badge-rim + .layui-badge-rim {
        margin-left: 5px;
    }
    #selCompany {
        display: none;
    }
</style>
<<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar layui-form-pane">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">关键字</label>
                        <div class="layui-input-inline mr0">
                            <input id="search_word" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnReset" class="layui-btn layui-btn-warm"><i class="layui-icon">&#xe669;</i>重置
                        </button>
                        <button id="btnAdd" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>
            <table class="layui-table" id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="userTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="reset">重置密码</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="userForm">
    <form lay-filter="userForm" class="layui-form model-form">
        <input name="user_id" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">账号</label>
            <div class="layui-input-block">
                <input name="username" placeholder="请输入账号" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item" id="newPsw">
            <label class="layui-form-label">密码:</label>
            <div class="layui-input-block">
                <input type="password" name="newPsw" id="newPswIn" placeholder="请输入密码" class="layui-input"
                       lay-verify="required|psw"/>
            </div>
        </div>
        <div class="layui-form-item" id="rePsw">
            <label class="layui-form-label">确认密码:</label>
            <div class="layui-input-block">
                <input type="password" name="rePsw" id="rePswIn" placeholder="请再次输入密码" class="layui-input"
                       lay-verify="required|psw"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input name="real_name" placeholder="请输入用户名" type="text" class="layui-input"
                       maxlength="20" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input name="phone" placeholder="请输入手机号" type="text" class="layui-input"
                       lay-verify="required|phone" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="男" title="男" checked/>
                <input type="radio" name="gender" value="女" title="女"/>
            </div>
        </div>
        <div class="layui-form-item" id="showRole">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <input type="text" id="role_name" name="role_name" hidden />
                <select name="role_id" id="role" lay-filter="role">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="userFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'formSelects', 'admin', 'config'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var formSelects = layui.formSelects;
        var admin = layui.admin;
        var config = layui.config;

        var roleArr = []; //全部角色

        form.render('select');
        var userInfo = config.getUser();
        var page = 1;

        // 渲染表格
        table.render({
            elem: '#userTable',
            url: serverUrlAdmin+'Manager/getManagerList.html',
            method:'POST',
            where: {
                search_word:$("#search_word").val(),
            },
            page: true,
            cols: [[
                {align: 'center',field: 'username',title: '账号'},
                {align: 'center',field: 'real_name',title: '姓名'},
                {align: 'center',field: 'phone',title: '手机号'},
                {align: 'center',field: 'gender',title: '性别'},
                {align: 'center',field: 'role_name',title: '角色'},
                {align: 'center',field: 'remark', title: '备注'},
                {align: 'center',field: 'create_time', title: '注册时间'},
                {align: 'center',toolbar: '#userTableBar',title: '操作',unresize: true}
            ]],
            done: function (res, curr, count) {
                page = res.page;
            }

        });
        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var search_word = $('#search_word').val();
            table.reload("userTable", {where: {search_word: search_word}});
        });

        $('#btnReset').click(function () {
            $('#search_word').val('');
            form.render();
            table.reload("userTable", {where: {search_word: ''}});
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });
        // 工具条点击事件
        table.on('tool(userTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') { // 删除
                layer.confirm('确定删除此用户吗？', {
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    $.post(serverUrlAdmin + 'Manager/del', {id:data.user_id}, function (res) {
                        layer.closeAll('loading');
                        if (res.success === true) {
                            layer.msg(res.obj, {icon: 1, time:300}, function () {
                                var search_word = $('#search_word').val();
                                table.reload('userTable',{page :{curr:page}}, {where: {search_word: search_word}});
                            });
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },'json');
                });
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定重置此用户的密码吗？<br><span style="color: red;">（初始密码为123456）</span>', {
                    skin: 'layui-layer-admin'
                }, function (i) {
                    layer.close(i);
                    layer.load(2);
                    $.post(serverUrlAdmin + 'Manager/reset_psw', {id:data.user_id}, function (res) {
                        layer.closeAll('loading');
                        if (res.success === true) {
                            layer.msg(res.obj, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },'json');
                });
            } else if (layEvent === 'edit') { // 修改
                showEditModel(data);
            }
        });

        // 表单提交
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '500px',
                offset: '65px',
                title: data ? '修改用户' : '添加用户',
                content: $('#userForm').html(),
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');

                    var roleStr='<option value="">请选择</option>';
                    for (var i in roleArr){
                        roleStr+='<option value="'+i+'">'+roleArr[i]+'</option>';
                    }
                    $('#role').html(roleStr);
                    if (data) {
                        $("#rePswIn").removeAttr('lay-verify');
                        $("#newPswIn").removeAttr('lay-verify');
                        $("#newPsw").hide();
                        $("#rePsw").hide();
                        form.val('userForm', data);
                    }
                    form.render();
                }
            });
        }

        form.on('select(role)', function(data){
            var role_id = data.value;
            $('#role_name').val(roleArr[role_id]);
            form.render();
        });

        form.on('submit(userFormSubmit)', function (d) {
            if(d.field.newPsw !== d.field.rePsw){
                layer.msg('两次密码输入不一致', {icon: 2});
            }
            $.post(serverUrlAdmin + 'Manager/manager_add_save', d.field, function (res) {
                layer.closeAll('loading');
                if (res.success === true) {
                    layer.msg(res.obj, {icon: 1});
                    layer.closeAll('page');
                    table.reload('userTable');
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },'json');
            return false;
        });

        // 获取全部角色
        layer.load(2);
        $.get(serverUrlAdmin + 'Sysrole/getAllList', function (res) {
            layer.closeAll('loading');
            if (res.success === true) {
                roleArr = res.obj;
            }
        },'json');

        // 添加表单验证方法
        form.verify({
            psw: [/^[\S]{5,20}$/, '密码必须5到20位，且不能出现空格'],
        });
        //缓存当前操作的是哪个表格的哪个tr的哪个td
        $(document).off('mousedown','.layui-table-grid-down').on('mousedown','.layui-table-grid-down',function (event) {
            //直接记录td的jquery对象
            table._tableTrCurrr = $(this).closest('td');
        });

        //给弹出的详情里面的按钮添加监听级联的触发原始table的按钮的点击事件
        $(document).off('click','.layui-table-tips-main [lay-event]').on('click','.layui-table-tips-main [lay-event]',function (event) {
            var elem = $(this);
            var tableTrCurrr =  table._tableTrCurrr;
            if(!tableTrCurrr){
                return;
            }
            var layerIndex = elem.closest('.layui-table-tips').attr('times');
            layer.close(layerIndex);
            table._tableTrCurrr.find('[lay-event="' + elem.attr('lay-event') + '"]').first().click();
        });
    });
</script>